package com.bruce.apkinstaller.other;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ParseApk {
    private static final String TAG = ParseApk.class.getSimpleName();

    public static Map<String, Object> getApkInfo(Context context, String path) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String archiveFilePath = path;// 安装包路径
            File file = new File(archiveFilePath);
            if (!file.exists()) {
                return map;
            }
            PackageManager pm = context.getPackageManager();
            PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath,
                    PackageManager.GET_ACTIVITIES);
            if (info != null) {
                ApplicationInfo appInfo = info.applicationInfo;
                String packageName = appInfo.packageName; // 得到安装包名称
                String version = info.versionName;        // 得到版本信息
                int versionCode = info.versionCode;

                map.put(ApkInfos.PK_NAME, packageName);
                map.put(ApkInfos.VE_NAME, version);
                map.put(ApkInfos.VE_CODE, versionCode);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        Log.d(TAG, map.toString());

        return map;
    }

    public class ApkInfos {
        public static final String PK_NAME = "packageName";
        public static final String VE_NAME = "versionName";
        public static final String VE_CODE = "versionCode";
    }
}
